package controller;

import com.google.gson.Gson;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import pojo.Admin;
import pojo.Blog;
import pojo.User;
import service.UserService;
import utils.util;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.Map;

//快捷键Alt+Eenter,Alt+Insert
/*@Controller*/
@RestController//相当于controller+responsebody,下面的方法中返回值给安卓端就不用加 @ResponseBody
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;

    @RequestMapping("/addUser")
    public String addSave(@RequestParam Map<String, String> map) throws NoSuchAlgorithmException {
        /*  String e = map.toString(); */
        String strUser = map.get("strUser");//获取安卓端传过来的键
        Gson gson = new Gson();
        User user = gson.fromJson(strUser, User.class);
        /*String w = user.toString();*/
        /*        System.out.print(e);*/
        user.setPassword(util.UseMD5(user.getPassword()));
        User userbyphone = userService.getUserByPhone(user.getPhone());
        if (userbyphone != null) {
            return "registered";          //存在用户
        } else {
            int i = userService.insert(user);
            if (i > 0) {
                return "sucess";   //保存成功
            } else {
                return "fail";     //保存失败
            }
        }
    }

    @RequestMapping("/updateInfo")
    public String updateInfo(@RequestParam Map<String, String> map) {
        String strUser = map.get("strUser");
        Gson gson = new Gson();
        User user = gson.fromJson(strUser, User.class);

        userService.updateInfo(user);
        return "success";
    }

    @RequestMapping("/updatepwd")
    public String updatepwd(@RequestParam Map<String, String> map) {
        User user = userService.getUserByPhone(map.get("phone"));
        try {
            if ((user.getPassword()).equals(util.UseMD5(map.get("password")))) {
                user.setPassword(util.UseMD5(map.get("newPwd")));
                userService.updatepwd(user);
            }
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "success";
    }

    @RequestMapping(value = "/login", produces = "text/html;charset=UTF-8")
    public String login(@RequestParam Map<String, String> map) throws NoSuchAlgorithmException {
        System.out.println("登陆成功");
        User user = userService.getUserByPhone(map.get("phone"));
        if (user != null) {
            String password = user.getPassword();
            if (password.equals(util.UseMD5(map.get("password")))) {
                String JsonStr = new Gson().toJson(user);
                return JsonStr;
            }
        }
        return "error";
    }

    @RequestMapping("FuzzyQueryUser")
    public void FuzzyQueryUser(@RequestParam Map<String,String> map, HttpServletResponse response){
        System.out.println("模糊查询");
        String userid=map.get("userid");

        List<User> list=userService.FuzzyQueryUser(userid);
        String JsonStr=new Gson().toJson(list);
        response.setCharacterEncoding("utf-8");
        System.out.println(JsonStr);
        try {
            response.getWriter().write(JsonStr);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @CrossOrigin
    @RequestMapping("getUserCount")
    public String getUserCount() {
        int blogNums = userService.getUserCount();
        String JsonStr = new Gson().toJson(blogNums);
        return JsonStr;

    }

    @CrossOrigin
    @RequestMapping("/selectWebUser")
    public void selectWebUser(HttpServletResponse response){
        System.out.println("页面数据查看被调用");
        List<User> list=userService.selectWebUser();
        String JsonStr=new Gson().toJson(list);
        response.setCharacterEncoding("utf-8");
        System.out.println(JsonStr);
        try {
            response.getWriter().write(JsonStr);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @CrossOrigin
    @RequestMapping("/delete")
    public void delete(String id){
        System.out.println("已删除");
        System.out.println("传过来的值："+id);
        int a=userService.delete(id);
        System.out.println("返回值"+a);

    }

    @CrossOrigin
    @RequestMapping("/deletes")
    public void deletes(String[] id){
        System.out.println("已删除");
        System.out.println("传过来的值："+id);
        int rows=userService.deletes(id);
        System.out.println("返回值"+rows);
    }



}
